<script>
import { getPicInfo, sendSmsCaptcha, smsCaptchaLogin } from '@/api/login'

export default {
  name: 'LoginPage',
  data () {
    return {
      picUrl: '', // 验证码图片
      picKey: '', // 验证码校验key
      picCode: '', // 验证码
      totalSecond: 60, // 总的秒数
      curSecond: 60, // 当前秒数
      mobile: '', // 手机号
      code: '', // 用户输入的验证码
      smsCode: '', // 用户输入的短信验证码
      timer: null
    }
  },

  created () {
    this.getPicCode()
  },

  destroyed () {
    // 如果倒计时没结束就离开了页面，就销毁这个倒计时任务
    clearInterval(this.timer)
  },

  methods: {
    async getPicCode () {
      // 封装请求到api目录下
      const res = await getPicInfo()
      const { data: { base64, key } } = res
      this.picUrl = base64
      this.picKey = key
    },

    // 校验手机号和验证码
    validLogin () {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.code)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },

    // 获取短信验证码
    async getMsgCode () {
      if (!this.validLogin()) {
        return
      }

      // 防止重复点击获取验证码
      if (this.timer) {
        return
      }

      // 调用发短信验证码接口
      await sendSmsCaptcha(this.code, this.picKey, this.mobile)
      this.$toast('短信发送成功，请注意查收')

      // 短信验证码倒计时
      this.timer = setInterval(() => {
        this.curSecond--
        if (this.curSecond <= 0) {
          clearInterval(this.timer)
          this.curSecond = this.totalSecond
          this.timer = null
        }
      }, 1000)
    },

    // 登录
    async login () {
      if (!this.validLogin()) {
        return
      }
      if (!/^\w{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的短信验证码')
        return
      }
      const res = await smsCaptchaLogin(this.mobile, this.smsCode)
      // 个人信息存入vuex并持久化到本地
      this.$store.commit('user/setUserInfo', res.data)
      // 跳转
      const backUrl = this.$route.query.backUrl
      if (backUrl) {
        this.$router.replace(backUrl)
      } else {
        this.$router.replace('/')
      }
      this.$toast('登录成功')
    }
  }
}
</script>

<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.back()"/>
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="code" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="picUrl" :src="picUrl" @click="getPicCode" alt="">
        </div>
        <div class="form-item">
          <input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getMsgCode">{{ curSecond === totalSecond ? '获取验证码' : curSecond + '秒后再次发送' }}
          </button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;

    h3 {
      font-size: 26px;
      font-weight: normal;
    }

    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;

    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }

    img {
      width: 94px;
      height: 31px;
    }

    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0, 0, 0, .1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
